<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['groups']=$this->getall('group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['pay_type']=$this->pay_type;
$return['gift_type']=$this->gift_type;
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_xunichong_goods',$params,array($pindex,$psize),$total,$fields,'','star desc,id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_xunichong/goods.detail//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品列表'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]:</br>1、商品类型--可以设置虚拟商品的（第三方虚拟支付码）！','edit'=>true,'list'=>array(
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'oldprice','title'=>'面值','type'=>'string'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string','width'=>'20'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','options'=>'status','width'=>'20'),
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'禁用'),array('id'=>'1','title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_xunichong_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_xunichong_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['images']=json_decode($item['images'],true);
	$item['starttime']=$item['starttime']?date('Y-m-d H:i',$item['starttime']):'';
	$item['endtime']=$item['endtime']?date('Y-m-d H:i',$item['endtime']):'';
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('name'=>'title','title'=>'商品名称','type'=>'string'),
								array('name'=>'desc','title'=>'商品描述','type'=>'string'),
								array('name'=>'image','title'=>'商品图片','type'=>'image'),
								array('name'=>'images','title'=>'商品轮播图','type'=>'images'),
								array('type'=>'radio','title'=>'商品类型','name'=>'type','options'=>'gift_type'),
								array('name'=>'oldprice','title'=>'面值','type'=>'string'),
								array('name'=>'price','title'=>'价格','type'=>'string'),
								array('name'=>'total','title'=>'库存','type'=>'string'),
								array('name'=>'unit','title'=>'单位','type'=>'string'),
								array('name'=>'groupid','title'=>'选择分类','type'=>'select','options'=>'groups'),
								array('type'=>'time','title'=>'开始时间','name'=>'starttime'),
								array('type'=>'time','title'=>'结束时间','name'=>'endtime'),
								array('name'=>'content','title'=>'内容','type'=>'richtext'),
							)
						),
			
			)
	);
	$return['gift_type']=$this->gift_type;
	$return['credit_type']=$this->credit_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('请设置商品名称','error');
	}
	if(!$postdata['groupid']){
		exi('请设置商品分类','error');
	}
	if($postdata['price']<=0){
		exi('价格必填','error');
	}
	if($postdata['oldprice']<=0){
		exi('面额必填','error');
	}
	$data=array(
		'type'=>intval($postdata['type']),
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'image'=>trim($postdata['image']),
		'images'=>ijson_encode($postdata['images']),
		'content'=>trim($postdata['content']),
		'groupid'=>intval($postdata['groupid']),
		'status'=>intval($postdata['status']),
		'price'=>trim($postdata['price']),
		'oldprice'=>intval($postdata['oldprice']),
		'total'=>intval($postdata['total']),
		'endtime'=>strtotime($postdata['endtime']),
		'starttime'=>strtotime($postdata['starttime']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_xunichong_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_xunichong_goods',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_xunichong_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的商品！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_xunichong_goods',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
